// import { addUserData } from '../api/users'
import { setToken, removeToken } from '@/utils/cookie'
export const state = () => {
    return {
        data: [],
        roles: [],
        userInfo: {}
    }
}
export const mutations = {
    SET_LIST(state, v) {
        state.data = v.data
    },
    SET_ROLES(state, roles) {
        state.roles = roles
    },
    SET_USER_INFO(state, obj) {
        state.userInfo = obj
    }
}

export const actions = {
    // 用户退出
    async doLogout({ commit }) {
        // 调用接口，删除token 
        commit('SET_ROLES', [])
        commit('SET_USER_INFO', [])
        removeToken()

    },
    // 根据token 得到用户相关信息
    async getUserInfo({ commit }, token) {
        const { data } = await this.$axios.get(`http://localhost:3001/users/user-info?token=${token}`)
        const { roles } = data.data
        commit('SET_ROLES', roles)
        commit('SET_USER_INFO', data.data)
    },
    // 用户登录
    async getData({ commit }) {
        const { data } = await this.$axios.get(
            "http://localhost:3001/users/user-list?pageSize=10"
        );
        commit('SET_LIST', data)
    },
    async addData({ }, row) {
        const r = await this.$axios.post('http://localhost:3001/users/save-user', row)
    },
    async editData({ }, row) {
        await this.$axios.post('http://localhost:3001/users/update-user', row)
    },
    async delData({ }, row) {
        console.log(row)
        await this.$axios.get(`http://localhost:3001/users/delete-user?id=${row}`)
    },
    async doLogin({ }, form) {
        const { data } = await this.$axios.post('http://localhost:3001/users/user-login', form)
        setToken(data.data.token)
        console.log(data.data.token)
    }

}

export const getters = {

}